package com.example.operation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.operation.dto.DecWaresDto;
import com.example.operation.service.DecWaresService;
import com.example.operation.utils.Result;
import com.example.operation.vo.DecWaresVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: DecWaresController
 * @Description:
 * @Author
 * @Date 2021/11/24
 * @Version 1.0
 */

@Api(tags = "出售中商品模块")
@RestController
@RequestMapping(value = "decwares")
public class DecWaresController {

    @Autowired
    private DecWaresService decWaresService;

    @ApiOperation(value = "商品分页模糊查询接口")
    @GetMapping("/page")
    public Result<Page<DecWaresVo>> pageResult (
            @RequestParam(value = "pageCurret") @ApiParam(value = "当前页码",required = true) Integer pageCurrent,
            @RequestParam(value = "pageSize") @ApiParam(value = "分页条数",required = true) Integer pageSize,
            @RequestParam(value = "pageNumber") @ApiParam(value = "分页页码",required = true) Integer pageNumber,
            @RequestParam(value = "title",required = false) @ApiParam(value = "商品名称") String title,
            @RequestParam(value = "offShelf") @ApiParam(value = "上下架状态切换",required = true) Integer offShelf){
        Page<DecWaresVo> decWaresVoPage = decWaresService.page(pageCurrent,pageSize,pageNumber,title,offShelf);
        return Result.ok(decWaresVoPage);
    }
    @ApiOperation(value = "发布商品接口")
    @PostMapping("/save")
    public Result<String> save(@RequestBody @Validated DecWaresDto decWaresDto){
        decWaresService.save(decWaresDto);
        return Result.ok("发布商品成功");
    }

    @ApiOperation(value = "修改商品接口")
    @PutMapping("/update")
    public Result<String> update(@RequestBody @Validated DecWaresDto decWaresDto){
        decWaresService.update(decWaresDto);
        return Result.ok("修改商品成功");
    }

    @ApiOperation(value = "商品详情查看规格表接口")
    @GetMapping("/selectById")
    public List<DecWaresVo> selectDwVo(@RequestParam(value = "id") Integer id){
        return decWaresService.selectDwVo(id);
    }

    @ApiOperation(value = "批量修改(上下架状态切换)接口")
    @PutMapping("/updateBatchById")
    public Result<String> updateBatchById(@RequestBody @Validated List<Integer> list,@RequestParam Integer offShelf){
        decWaresService.updateBatchById(list,offShelf);
        if(offShelf ==0){
            return Result.ok("商品上架成功");
        }
        else if(offShelf ==1){
            return Result.ok("商品下架成功");
        }
        return Result.ok("商品上下架格式错误");
}

    @ApiOperation(value = "批量删除接口")
    @DeleteMapping("/removeByIds")
    public Result<String> removeByIds(@RequestParam @Validated List<Integer> idList){
        decWaresService.removeByIds1(idList);
        return Result.ok("批量删除成功");
    }

}
